package com.heu.blood.analysis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.analysis.dao.BloodTestTransfusionDao;
import com.heu.blood.analysis.entity.BloodTestTransfusionEntity;
import com.heu.blood.analysis.entity.TestResult;
import com.heu.blood.analysis.exception.AnalysisExceptionEnum;
import com.heu.blood.analysis.service.BloodSampleInfoTransfusionService;
import com.heu.blood.analysis.service.BloodTestTransfusionService;
import com.heu.blood.analysis.vo.BloodTestAndChargeVo;
import com.heu.blood.analysis.vo.BloodTestTransfusionApplicationVo;
import com.heu.blood.analysis.vo.BloodTestTransfusionVo;
import com.heu.blood.analysis.vo.BloodTestqueryPatientTestResultVo;
import com.heu.blood.application.dao.BloodApplicationDao;
import com.heu.blood.application.dictenum.ApplicationStateEnum;
import com.heu.blood.application.dictenum.ApplicationVerifyStateEnum;
import com.heu.blood.application.entity.BloodApplicationEntity;
import com.heu.blood.application.service.BloodApplicationService;
import com.heu.blood.charge.dictenum.BloodChargeTypeEnum;
import com.heu.blood.charge.entity.BloodChargeEntity;
import com.heu.blood.charge.service.BloodChargeService;
import com.heu.blood.common.commponent.DictionaryLoader;
import com.heu.blood.common.enums.BloodChargeEnum;
import com.heu.blood.common.enums.BloodTestTransfusionStatusEnum;
import com.heu.blood.common.enums.DictDataEnum;
import com.heu.blood.common.enums.OperateStateEnum;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;
import com.heu.blood.examine.dao.OperateExamineDao;
import com.heu.blood.examine.entity.OperateExamineEntity;
import com.heu.blood.globalExceptionHandler.MyException;
import com.heu.blood.match.dao.BloodMatchDao;
import com.heu.blood.match.exception.TimeException;
import com.heu.blood.match.vo.BloodMatchRecordQueryBagDimensionVo;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.heu.blood.common.enums.CommonEnum.IF_SHOW;


@Service("bloodTestTransfusionService")
public class BloodTestTransfusionServiceImpl extends ServiceImpl<BloodTestTransfusionDao, BloodTestTransfusionEntity> implements BloodTestTransfusionService {

    @Autowired
    BloodTestTransfusionDao bloodTestTransfusionDao;

    @Autowired
    BloodSampleInfoTransfusionService bloodSampleInfoTransfusionService;

    @Autowired
    private BloodApplicationDao bloodApplicationDao;

    @Autowired
    private DictionaryLoader dictionaryLoader;

    @Autowired
    private BloodMatchDao bloodMatchDao;

    @Autowired
    private BloodChargeService bloodChargeService;

    @Autowired
    private OperateExamineDao operateExamineDao;
    @Autowired
    BloodApplicationService bloodApplicationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BloodTestTransfusionEntity> page = this.page(
                new Query<BloodTestTransfusionEntity>().getPage(params),
                new QueryWrapper<BloodTestTransfusionEntity>()
        );

        return new PageUtils(page);
    }
// 血液样本和血液检测结果联表查询
    @Override
    public PageUtils selectSamplePatientTestPage(Map<String, Object> params) {
//        创建bloodSampleInfoTransfusionVo接收前端的查询条件
        BloodTestTransfusionVo bloodTestTransfusionVo = new BloodTestTransfusionVo();
        if (Strings.isNotBlank((String) params.get("bloodTestTransfusionId"))) {
            bloodTestTransfusionVo.setBloodTestTransfusionId((String) params.get
                    ("bloodTestTransfusionId")); // 检测单号
        }
//        1.get2.set3.get
        if (Strings.isNotBlank((String) params.get("bloodSampleInfoTransfusionId"))) {
            bloodTestTransfusionVo.setBloodSampleInfoTransfusionId((String) params.get
                    ("bloodSampleInfoTransfusionId")); // 输血申请的血液样本ID
        }
        if (Strings.isNotBlank((String) params.get("inspector"))) {
            bloodTestTransfusionVo.setInspector((String) params.get
                    ("inspector"));// 检测人
        }
        if (Strings.isNotBlank((String) params.get("patientId"))) {
            bloodTestTransfusionVo.setPatientId((String) params.get
                    ("patientId"));// 根据病人编号查询
        }
        if (Strings.isNotBlank((String) params.get("patientName"))) {
            bloodTestTransfusionVo.setPatientName((String) params.get
                    ("patientName"));// 根据病人姓名查询
        }
//        if (Strings.isNotBlank((String) params.get("collector"))) {
//            bloodTestTransfusionVo.setCollector((String) params.get
//                    ("collector")); // 采血者
//        }
//        if (Strings.isNotBlank((String) params.get("recipient"))) {
//            bloodTestTransfusionVo.setRecipient((String) params.get
//                    ("recipient")); // 接收者
//        }
        if (Strings.isNotBlank((String) params.get("reason"))) {
            bloodTestTransfusionVo.setReason((String) params.get
                    ("reason")); // 退血原因
        }
        if (Strings.isNotBlank((String) params.get("department"))) {
            bloodTestTransfusionVo.setDepartment((String) params.get
                    ("department")); // 部门
        }
        if (Strings.isNotBlank((String) params.get("applicationProject"))) {
            bloodTestTransfusionVo.setApplicationProject((String) params.get
                    ("applicationProject")); // 申请项目
        }
//        if (Strings.isNotBlank((String) params.get("inspectStatus"))) {
//            bloodTestTransfusionVo.setInspectStatus((String) params.get
//                    ("inspectStatus")); // 项目状态
//        }
        // 添加检测状态查询条件
        if (Strings.isNotBlank((String) params.get("inspectStatus"))) {
            bloodTestTransfusionVo.setInspectStatus((String) params.get("inspectStatus")); // 检测状态
        }
        if (Strings.isNotBlank((String) params.get("recipient"))) {
            bloodTestTransfusionVo.setRecipient((String) params.get("recipient")); // 签收人
        }

        // 接收时间范围条件
        String inspectStartTime = null; // 检测时间左边界
        String inspectEndTime = null; // 检测时间有边界
        String receivedStartTime = null; // 接收时间左边界
        String receivedEndTime = null; // 接收时间右边界
        String collectStartTime = null; // 采血时间左边界
        String collectEndTime = null; // 采血时间右边界

        if (Strings.isNotBlank((String) params.get("inspectStartTime"))){
            inspectStartTime = (String) params.get("inspectStartTime");
        }
        if (Strings.isNotBlank((String) params.get("inspectEndTime"))){
            inspectEndTime = (String) params.get("inspectEndTime");
        }
        if (Strings.isNotBlank(inspectStartTime) && Strings.isNotBlank(inspectEndTime)) {
            if (inspectStartTime.compareTo(inspectEndTime) > 0) {
                throw new MyException(TimeException.TIME_OUTLINE);
            }
        }
        if (Strings.isNotBlank((String) params.get("receivedStartTime"))){
            receivedStartTime = (String) params.get("receivedStartTime");
        }
        if (Strings.isNotBlank((String) params.get("receivedEndTime"))){
            receivedEndTime = (String) params.get("receivedEndTime");
        }
        if (Strings.isNotBlank(receivedStartTime) && Strings.isNotBlank(receivedEndTime)) {
            if (receivedStartTime.compareTo(receivedEndTime) > 0) {
                throw new MyException(TimeException.TIME_OUTLINE);
            }
        }
        if (Strings.isNotBlank((String) params.get("collectStartTime"))){
            collectStartTime = (String) params.get("collectStartTime");
        }
        if (Strings.isNotBlank((String) params.get("collectEndTime"))){
            collectEndTime = (String) params.get("collectEndTime");
        }
        if (Strings.isNotBlank(collectStartTime) && Strings.isNotBlank(collectEndTime)) {
            if (collectStartTime.compareTo(collectEndTime) > 0) {
                throw new MyException(TimeException.TIME_OUTLINE);
            }
        }
        QueryWrapper<BloodTestTransfusionVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(bloodTestTransfusionVo.
                getBloodTestTransfusionId()), "bt.blood_test_transfusion_id", bloodTestTransfusionVo.
                getBloodTestTransfusionId())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo.
                        getBloodSampleInfoTransfusionId()), "bt.blood_sample_info_transfusion_id", bloodTestTransfusionVo.
                        getBloodSampleInfoTransfusionId())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo.
                        getInspector()), "bt.inspector", bloodTestTransfusionVo.
                        getInspector())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo.
                        getPatientId()), "bs.patient_id", bloodTestTransfusionVo.
                        getPatientId())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo.
                        getPatientName()), "bs.patient_name", bloodTestTransfusionVo.
                        getPatientName())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo.
                        getReason()), "bs.reason", bloodTestTransfusionVo.
                        getReason())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo.
                        getDepartment()), "bs.department", bloodTestTransfusionVo.
                        getDepartment())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo.
                        getApplicationProject()), "bt.application_project", bloodTestTransfusionVo.
                        getApplicationProject())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo
                        .getInspectStatus()), "bt.inspect_status", bloodTestTransfusionVo.
                        getInspectStatus())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo.
                        getRecipient()), "bs.recipient", bloodTestTransfusionVo.
                        getRecipient())
                .ge(Strings.isNotBlank(inspectStartTime), "bt.inspect_time", inspectStartTime)
                .le(Strings.isNotBlank(inspectEndTime), "bt.inspect_time", inspectEndTime)
                .ge(Strings.isNotBlank(collectStartTime), "bs.collect_time", collectStartTime)
                .le(Strings.isNotBlank(collectEndTime), "bs.collect_time", collectEndTime)
                .ge(Strings.isNotBlank(receivedStartTime), "bs.received_time", receivedStartTime)
                .le(Strings.isNotBlank(receivedEndTime), "bs.received_time", receivedEndTime);

        IPage<BloodTestTransfusionVo> page = bloodTestTransfusionDao.selectBloodTestTransfusionPage(
                new Query<BloodTestTransfusionVo>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }
// 根据输血申请单单号查询相应的检验结果
    @Override
    public PageUtils selectBloodTestTransfusionApplicationPage(Map<String, Object> params) {
        //        创建BloodTestTransfusionApplicationVo接收前端的查询条件
        BloodTestTransfusionApplicationVo bloodTestTransfusionVo = new BloodTestTransfusionApplicationVo();
        if (Strings.isNotBlank((String) params.get("transfusionId"))) {
            bloodTestTransfusionVo.setApplicationId((String) params.get
                    ("transfusionId")); // 输血申请单
        }

        if (Strings.isNotBlank((String) params.get("bloodTestTransfusionId"))) {
            bloodTestTransfusionVo.setBloodTestTransfusionId((String) params.get
                    ("bloodTestTransfusionId")); // 检测单号
        }
//        1.get2.set3.get
        if (Strings.isNotBlank((String) params.get("bloodSampleInfoTransfusionId"))) {
            bloodTestTransfusionVo.setBloodSampleInfoTransfusionId((String) params.get
                    ("bloodSampleInfoTransfusionId")); // 输血申请的血液样本ID
        }
        if (Strings.isNotBlank((String) params.get("inspector"))) {
            bloodTestTransfusionVo.setInspector((String) params.get
                    ("inspector"));// 检测人
        }
        if (Strings.isNotBlank((String) params.get("patientId"))) {
            bloodTestTransfusionVo.setPatientId((String) params.get
                    ("patientId"));// 根据病人编号查询
        }

        if (Strings.isNotBlank((String) params.get("department"))) {
            bloodTestTransfusionVo.setDepartment((String) params.get
                    ("department")); // 部门
        }
        if (Strings.isNotBlank((String) params.get("applicationProject"))) {
            bloodTestTransfusionVo.setApplicationProject((String) params.get
                    ("applicationProject")); // 申请项目
        }

        String inspectStartTime = null; // 检测时间左边界
        String inspectEndTime = null; // 检测时间有边界
//        String collectStartTime = null; // 采血时间左边界
//        String collectEndTime = null; // 采血时间右边界
        String applicationStartTime = null; // 申请时间左边界
        String applicationEndTime = null; // 申请时间右边界
        String transfusionStartTime = null; // 输血时间左边界
        String transfusionEndTime = null; // 输血时间右边界


        if (Strings.isNotBlank((String) params.get("inspectStartTime"))){
            inspectStartTime = (String) params.get("inspectStartTime");
        }
        if (Strings.isNotBlank((String) params.get("inspectEndTime"))){
            inspectEndTime = (String) params.get("inspectEndTime");
        }
//        if (Strings.isNotBlank((String) params.get("collectStartTime"))){
//            collectStartTime = (String) params.get("collectStartTime");
//        }
//        if (Strings.isNotBlank((String) params.get("collectEndTime"))){
//            collectEndTime = (String) params.get("collectEndTime");
//        }
        if (Strings.isNotBlank((String) params.get("applicationStartTime"))){
            applicationStartTime = (String) params.get("applicationStartTime");
        }
        if (Strings.isNotBlank((String) params.get("applicationEndTime"))){
            applicationEndTime = (String) params.get("applicationEndTime");
        }
        if (Strings.isNotBlank((String) params.get("transfusionStartTime"))){
            transfusionStartTime = (String) params.get("transfusionStartTime");
        }
        if (Strings.isNotBlank((String) params.get("transfusionEndTime"))){
            transfusionEndTime = (String) params.get("transfusionEndTime");
        }
        QueryWrapper<BloodTestTransfusionApplicationVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(bloodTestTransfusionVo.
                getBloodTestTransfusionId()), "bt.blood_test_transfusion_id", bloodTestTransfusionVo.
                getBloodTestTransfusionId())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo.
                        getApplicationId()), "bt.application_id", bloodTestTransfusionVo.
                        getApplicationId())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo.
                        getBloodSampleInfoTransfusionId()), "bt.blood_sample_info_transfusion_id", bloodTestTransfusionVo.
                        getBloodSampleInfoTransfusionId())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo.
                        getInspector()), "bt.inspector", bloodTestTransfusionVo.
                        getInspector())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo.
                        getPatientId()), "ta.patient_id", bloodTestTransfusionVo.
                        getPatientId())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo.
                        getDepartment()), "ta.department", bloodTestTransfusionVo.
                        getDepartment())
                .eq(Strings.isNotBlank(bloodTestTransfusionVo.
                        getApplicationProject()), "bt.application_project", bloodTestTransfusionVo.
                        getApplicationProject())
                .ge(Strings.isNotBlank(inspectStartTime), "bt.inspect_time", inspectStartTime)
                .le(Strings.isNotBlank(inspectEndTime), "bt.inspect_time", inspectEndTime)
                .ge(Strings.isNotBlank(applicationStartTime), "ta.application_time", applicationStartTime)
                .le(Strings.isNotBlank(applicationEndTime), "ta.application_time", applicationEndTime)
                .ge(Strings.isNotBlank(transfusionStartTime), "ta.transfusion_time", transfusionStartTime)
                .le(Strings.isNotBlank(transfusionEndTime), "ta.transfusion_time", transfusionEndTime);


        IPage<BloodTestTransfusionApplicationVo> page = bloodTestTransfusionDao.selectBloodTestTransfusionApplicationPage(
                new Query<BloodTestTransfusionApplicationVo>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public BloodTestqueryPatientTestResultVo queryPatientTestResult(String bloodSampleId) {
//        1.获取血液样本信息
//        BloodSampleTransfusionReportVo bloodSampleTransfusionReportVo = bloodSampleInfoTransfusionService.
//                selectSamplePatientReport(bloodSampleId);
//        if (bloodSampleTransfusionReportVo == null){
//
//        }
//        1.获取配血记录
        List<BloodMatchRecordQueryBagDimensionVo> bloodMatchRecordQueryBagDimensionVos = bloodMatchDao.selectBloodMatchRecordByBloodSampleInfoTransfusionId(bloodSampleId);
//        2.获取检测表信息
        List<BloodTestTransfusionEntity> bloodTestEntityList = this.getBySampleId(bloodSampleId);
        if (bloodTestEntityList == null){
            return null;
        }
        // 3、设定一个List来存放查询出来的检测项目和检测结果<检测项目，<检测项目价格，检测结果>>
        List<TestResult> testResults = new ArrayList<>();

        Map<String, Map<String, String[]>> dict = dictionaryLoader.getDict();
        // 4、
        for(BloodTestTransfusionEntity bloodTestTransfusionEntity:bloodTestEntityList) {
            //创建TestResult类用于存放检测结果
            TestResult testResult = new TestResult();
            //写入检测项目,检测结果,检测次数,价格
//            字典里缺少项目直接抛异常
            if (dict.get(DictDataEnum.PAYMENT_PROJECT.code()).get(bloodTestTransfusionEntity.getApplicationProject()) == null) {
                throw new MyException(AnalysisExceptionEnum.DICT_NULL_POINTER);
            }
            testResult.setProjectName(dict.get(DictDataEnum.PAYMENT_PROJECT.code()).get(bloodTestTransfusionEntity.getApplicationProject())[0]);
            testResult.setResult(bloodTestTransfusionEntity.getProjectResult());
            testResult.setCount(bloodTestTransfusionEntity.getCount());
            testResult.setPrice(bloodTestTransfusionEntity.getPrice());
            //将检测结果保存
            testResults.add(testResult);
        }
        // 5、再将相关检测与病人信息封装成一个新的Vo返回
        BloodTestqueryPatientTestResultVo bloodTestqueryPatientTestResultVo = new BloodTestqueryPatientTestResultVo();
        bloodTestqueryPatientTestResultVo.setTestResults(testResults);
        bloodTestqueryPatientTestResultVo.setBloodMatchRecordQueryBagDimensionVos(bloodMatchRecordQueryBagDimensionVos);

        return bloodTestqueryPatientTestResultVo;
    }

//    根据sampleId查询血液样本的检测表
    @Override
    public List<BloodTestTransfusionEntity> getBySampleId(String bloodSampleId) {
        LambdaQueryWrapper<BloodTestTransfusionEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(bloodSampleId), BloodTestTransfusionEntity::getBloodSampleInfoTransfusionId, bloodSampleId)
                .eq(BloodTestTransfusionEntity::getIfShow, IF_SHOW.code());
        return bloodTestTransfusionDao.selectList(queryWrapper);
    }

    @Override
    public List<BloodTestTransfusionEntity> getTestResWithSampleId(String bloodSampleInfoTransfusionId) {
        LambdaQueryWrapper<BloodTestTransfusionEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(bloodSampleInfoTransfusionId),BloodTestTransfusionEntity::getBloodSampleInfoTransfusionId,bloodSampleInfoTransfusionId);
        List<BloodTestTransfusionEntity> bloodTestTransfusionEntities = bloodTestTransfusionDao.selectList(queryWrapper);
        return bloodTestTransfusionEntities;
    }

    @Override
    @Transactional
    public int saveBatchWithCharge(BloodTestAndChargeVo bloodTestAndChargeVo) {
        // 1.首先把检测项全部都保存到数据库
        List<BloodTestTransfusionEntity> bloodTestTransfusionList = bloodTestAndChargeVo.getBloodTestTransfusion();

        // 为每个检测项设置检测状态为待检测
        for(BloodTestTransfusionEntity entity : bloodTestTransfusionList){
            entity.setInspectStatus(BloodTestTransfusionStatusEnum.PENDING_INSPECTION.code());
        }
        this.saveBatch(bloodTestTransfusionList);
        // 2.把收费项目全部保存到数据库中
        List<BloodChargeEntity> bloodChargeEntityList = new ArrayList<>();
        for(BloodTestTransfusionEntity entity : bloodTestTransfusionList){
            BloodChargeEntity chargeEntity = new BloodChargeEntity();
            // 把属性全部拷贝进要保存的里面
            BeanUtils.copyProperties(bloodTestAndChargeVo,chargeEntity);
            // 拷贝一下申请项目
            chargeEntity.setPayment(entity.getApplicationProject());
            chargeEntity.setUnitPrice(entity.getPerPrice()); // 设置单价
            chargeEntity.setQuantity(entity.getCount()); // 设置数量
            chargeEntity.setTotalPrice(entity.getPrice().toString()); // 设置总价
            chargeEntity.setApplyDepartment(bloodTestAndChargeVo.getDepartment());
            chargeEntity.setChargeState(BloodChargeEnum.CHARGE_UNVERIFIED.code());
            chargeEntity.setChargeType(BloodChargeTypeEnum.INSPECTION_FEE.code());
            bloodChargeEntityList.add(chargeEntity);
        }

        bloodChargeService.saveBatch(bloodChargeEntityList);
        return 0;
    }

    @Override
    public int updateInspectResult(List<BloodTestTransfusionEntity> bloodTestTransfusionEntityList) {
        for(BloodTestTransfusionEntity entity : bloodTestTransfusionEntityList){
            entity.setInspectStatus(BloodTestTransfusionStatusEnum.INSPECTION_COMPLETED.code());
            LambdaUpdateWrapper<BloodApplicationEntity> bloodApplicationEntityUpdateWrapper = new LambdaUpdateWrapper<>();
            bloodApplicationEntityUpdateWrapper.set(BloodApplicationEntity::getApplicationState,ApplicationStateEnum.SEND_GET_BLOOD.code());
            bloodApplicationEntityUpdateWrapper.eq(Strings.isNotBlank(entity.getApplicationId()),BloodApplicationEntity::getApplicationId,entity.getApplicationId());
            bloodApplicationService.update(bloodApplicationEntityUpdateWrapper);
        }
        // 批量更新
        this.updateBatchById(bloodTestTransfusionEntityList);
        return 0;
    }

    @Override
    public int checkReviewResult(String bloodSampleInfoTransfusionId) {
        if (!Strings.isNotBlank(bloodSampleInfoTransfusionId) || "null".equalsIgnoreCase(bloodSampleInfoTransfusionId)) {
            throw new MyException(AnalysisExceptionEnum.NO_SAMPLE_LINKED);
        }

        LambdaQueryWrapper<BloodApplicationEntity> applicationQuery = new LambdaQueryWrapper<>();
        applicationQuery.eq(Strings.isNotBlank(bloodSampleInfoTransfusionId), BloodApplicationEntity::getBloodSampleInfoTransfusionId, bloodSampleInfoTransfusionId)
                .orderByDesc(BloodApplicationEntity::getGmtCreate);
        List<BloodApplicationEntity> applicationEntityList = bloodApplicationDao.selectList(applicationQuery);
        if (applicationEntityList.isEmpty()) {
            throw new MyException(AnalysisExceptionEnum.NO_APPLICATION);
        }
        BloodApplicationEntity applicationEntity = applicationEntityList.get(0);

        String applicationId = applicationEntity.getApplicationId();

        LambdaQueryWrapper<OperateExamineEntity> examineQuery = new LambdaQueryWrapper<>();
        examineQuery.eq(Strings.isNotBlank(applicationId), OperateExamineEntity::getTradeId, applicationId);
        List<OperateExamineEntity> examineEntityList = operateExamineDao.selectList(examineQuery);

        for (OperateExamineEntity operateExamineEntity: examineEntityList){
            if (! OperateStateEnum.PASS.code().equals(operateExamineEntity.getOperateState())){
                throw new MyException(AnalysisExceptionEnum.NOT_FULLY_APPROVED);
            }
        }
        return 0;
    }
}
